using HKB.Common;
using HKB.Data.Model;
using System;
using System.Data;
using System.Data.SqlClient;
using System.Text;
namespace HKB.Data.Dal
{
	public class CouponItem
	{
		public int GetMaxId()
		{
			return Conn.GetMaxID("CouponItemID", "HL_CouponItem");
		}
		public bool Exists(int CouponItemID)
		{
			System.Text.StringBuilder stringBuilder = new System.Text.StringBuilder();
			stringBuilder.Append("select count(1) from HL_CouponItem");
			stringBuilder.Append(" where CouponItemID=@CouponItemID");
			SqlParameter[] array = new SqlParameter[]
			{
				new SqlParameter("@CouponItemID", SqlDbType.Int, 4)
			};
			array[0].Value = CouponItemID;
			return Conn.Exists(stringBuilder.ToString(), array);
		}
		public int Add(HKB.Data.Model.CouponItem model)
		{
			System.Text.StringBuilder stringBuilder = new System.Text.StringBuilder();
			stringBuilder.Append("insert into HL_CouponItem(");
			stringBuilder.Append("CouponInfoID,CouponNum,UserName,GainOrderID,UseOrderID,GainDate,UseDate,UseStatus,EmailStatus,MobileStatus,MessageStatus)");
			stringBuilder.Append(" values (");
			stringBuilder.Append("@CouponInfoID,@CouponNum,@UserName,@GainOrderID,@UseOrderID,@GainDate,@UseDate,@UseStatus,@EmailStatus,@MobileStatus,@MessageStatus)");
			stringBuilder.Append(";select @@IDENTITY");
			SqlParameter[] array = new SqlParameter[]
			{
				new SqlParameter("@CouponInfoID", SqlDbType.Int, 4),
				new SqlParameter("@CouponNum", SqlDbType.NVarChar, 30),
				new SqlParameter("@UserName", SqlDbType.NVarChar, 30),
				new SqlParameter("@GainOrderID", SqlDbType.Int, 4),
				new SqlParameter("@UseOrderID", SqlDbType.Int, 4),
				new SqlParameter("@GainDate", SqlDbType.DateTime),
				new SqlParameter("@UseDate", SqlDbType.DateTime),
				new SqlParameter("@UseStatus", SqlDbType.Bit, 1),
				new SqlParameter("@EmailStatus", SqlDbType.Bit, 1),
				new SqlParameter("@MobileStatus", SqlDbType.Bit, 1),
				new SqlParameter("@MessageStatus", SqlDbType.Bit, 1)
			};
			array[0].Value = model.CouponInfoID;
			array[1].Value = model.CouponNum;
			array[2].Value = model.UserName;
			array[3].Value = model.GainOrderID;
			array[4].Value = model.UseOrderID;
			array[5].Value = model.GainDate;
			array[6].Value = model.UseDate;
			array[7].Value = model.UseStatus;
			array[8].Value = model.EmailStatus;
			array[9].Value = model.MobileStatus;
			array[10].Value = model.MessageStatus;
			object single = Conn.GetSingle(stringBuilder.ToString(), array);
			int result;
			if (single == null)
			{
				result = 0;
			}
			else
			{
				result = System.Convert.ToInt32(single);
			}
			return result;
		}
		public bool Update(HKB.Data.Model.CouponItem model)
		{
			System.Text.StringBuilder stringBuilder = new System.Text.StringBuilder();
			stringBuilder.Append("update HL_CouponItem set ");
			stringBuilder.Append("CouponInfoID=@CouponInfoID,");
			stringBuilder.Append("CouponNum=@CouponNum,");
			stringBuilder.Append("UserName=@UserName,");
			stringBuilder.Append("GainOrderID=@GainOrderID,");
			stringBuilder.Append("UseOrderID=@UseOrderID,");
			stringBuilder.Append("GainDate=@GainDate,");
			stringBuilder.Append("UseDate=@UseDate,");
			stringBuilder.Append("UseStatus=@UseStatus,");
			stringBuilder.Append("EmailStatus=@EmailStatus,");
			stringBuilder.Append("MobileStatus=@MobileStatus,");
			stringBuilder.Append("MessageStatus=@MessageStatus");
			stringBuilder.Append(" where CouponItemID=@CouponItemID");
			SqlParameter[] array = new SqlParameter[]
			{
				new SqlParameter("@CouponInfoID", SqlDbType.Int, 4),
				new SqlParameter("@CouponNum", SqlDbType.NVarChar, 30),
				new SqlParameter("@UserName", SqlDbType.NVarChar, 30),
				new SqlParameter("@GainOrderID", SqlDbType.Int, 4),
				new SqlParameter("@UseOrderID", SqlDbType.Int, 4),
				new SqlParameter("@GainDate", SqlDbType.DateTime),
				new SqlParameter("@UseDate", SqlDbType.DateTime),
				new SqlParameter("@UseStatus", SqlDbType.Bit, 1),
				new SqlParameter("@EmailStatus", SqlDbType.Bit, 1),
				new SqlParameter("@MobileStatus", SqlDbType.Bit, 1),
				new SqlParameter("@MessageStatus", SqlDbType.Bit, 1),
				new SqlParameter("@CouponItemID", SqlDbType.Int, 4)
			};
			array[0].Value = model.CouponInfoID;
			array[1].Value = model.CouponNum;
			array[2].Value = model.UserName;
			array[3].Value = model.GainOrderID;
			array[4].Value = model.UseOrderID;
			array[5].Value = model.GainDate;
			array[6].Value = model.UseDate;
			array[7].Value = model.UseStatus;
			array[8].Value = model.EmailStatus;
			array[9].Value = model.MobileStatus;
			array[10].Value = model.MessageStatus;
			array[11].Value = model.CouponItemID;
			int num = Conn.ExecuteSql(stringBuilder.ToString(), array);
			return num > 0;
		}
		public bool Delete(int CouponItemID)
		{
			System.Text.StringBuilder stringBuilder = new System.Text.StringBuilder();
			stringBuilder.Append("delete from HL_CouponItem ");
			stringBuilder.Append(" where CouponItemID=@CouponItemID");
			SqlParameter[] array = new SqlParameter[]
			{
				new SqlParameter("@CouponItemID", SqlDbType.Int, 4)
			};
			array[0].Value = CouponItemID;
			int num = Conn.ExecuteSql(stringBuilder.ToString(), array);
			return num > 0;
		}
		public bool DeleteList(string CouponItemIDlist)
		{
			System.Text.StringBuilder stringBuilder = new System.Text.StringBuilder();
			stringBuilder.Append("delete from HL_CouponItem ");
			stringBuilder.Append(" where CouponItemID in (" + CouponItemIDlist + ")  ");
			int num = Conn.ExecuteSql(stringBuilder.ToString(), new IDbDataParameter[0]);
			return num > 0;
		}
		public HKB.Data.Model.CouponItem GetModel(int CouponItemID)
		{
			System.Text.StringBuilder stringBuilder = new System.Text.StringBuilder();
			stringBuilder.Append("select  top 1 * from HL_CouponItem ");
			stringBuilder.Append(" where CouponItemID=@CouponItemID");
			SqlParameter[] array = new SqlParameter[]
			{
				new SqlParameter("@CouponItemID", SqlDbType.Int, 4)
			};
			array[0].Value = CouponItemID;
			HKB.Data.Model.CouponItem couponItem = new HKB.Data.Model.CouponItem();
			DataSet dataSet = Conn.Query(stringBuilder.ToString(), array);
			HKB.Data.Model.CouponItem result;
			if (dataSet.Tables[0].Rows.Count > 0)
			{
				result = this.DataRowToModel(dataSet.Tables[0].Rows[0]);
			}
			else
			{
				result = null;
			}
			return result;
		}
		public HKB.Data.Model.CouponItem DataRowToModel(DataRow row)
		{
			HKB.Data.Model.CouponItem couponItem = new HKB.Data.Model.CouponItem();
			if (row != null)
			{
				if (row["CouponItemID"] != null && row["CouponItemID"].ToString() != "")
				{
					couponItem.CouponItemID = int.Parse(row["CouponItemID"].ToString());
				}
				if (row["CouponInfoID"] != null && row["CouponInfoID"].ToString() != "")
				{
					couponItem.CouponInfoID = new int?(int.Parse(row["CouponInfoID"].ToString()));
				}
				if (row["CouponNum"] != null)
				{
					couponItem.CouponNum = row["CouponNum"].ToString();
				}
				if (row["UserName"] != null)
				{
					couponItem.UserName = row["UserName"].ToString();
				}
				if (row["GainOrderID"] != null && row["GainOrderID"].ToString() != "")
				{
					couponItem.GainOrderID = new int?(int.Parse(row["GainOrderID"].ToString()));
				}
				if (row["UseOrderID"] != null && row["UseOrderID"].ToString() != "")
				{
					couponItem.UseOrderID = new int?(int.Parse(row["UseOrderID"].ToString()));
				}
				if (row["GainDate"] != null && row["GainDate"].ToString() != "")
				{
					couponItem.GainDate = new System.DateTime?(System.DateTime.Parse(row["GainDate"].ToString()));
				}
				if (row["UseDate"] != null && row["UseDate"].ToString() != "")
				{
					couponItem.UseDate = new System.DateTime?(System.DateTime.Parse(row["UseDate"].ToString()));
				}
				if (row["UseStatus"] != null && row["UseStatus"].ToString() != "")
				{
					if (row["UseStatus"].ToString() == "1" || row["UseStatus"].ToString().ToLower() == "true")
					{
						couponItem.UseStatus = true;
					}
					else
					{
						couponItem.UseStatus = false;
					}
				}
				if (row["EmailStatus"] != null && row["EmailStatus"].ToString() != "")
				{
					if (row["EmailStatus"].ToString() == "1" || row["EmailStatus"].ToString().ToLower() == "true")
					{
						couponItem.EmailStatus = true;
					}
					else
					{
						couponItem.EmailStatus = false;
					}
				}
				if (row["MobileStatus"] != null && row["MobileStatus"].ToString() != "")
				{
					if (row["MobileStatus"].ToString() == "1" || row["MobileStatus"].ToString().ToLower() == "true")
					{
						couponItem.MobileStatus = true;
					}
					else
					{
						couponItem.MobileStatus = false;
					}
				}
				if (row["MessageStatus"] != null && row["MessageStatus"].ToString() != "")
				{
					if (row["MessageStatus"].ToString() == "1" || row["MessageStatus"].ToString().ToLower() == "true")
					{
						couponItem.MessageStatus = true;
					}
					else
					{
						couponItem.MessageStatus = false;
					}
				}
			}
			return couponItem;
		}
		public DataSet GetList(string strWhere)
		{
			System.Text.StringBuilder stringBuilder = new System.Text.StringBuilder();
			stringBuilder.Append("select * FROM HL_CouponItem ");
			if (strWhere.Trim() != "")
			{
				stringBuilder.Append(" where " + strWhere);
			}
			return Conn.Query(stringBuilder.ToString(), new IDbDataParameter[0]);
		}
		public DataSet GetList(int Top, string strWhere, string filedOrder)
		{
			System.Text.StringBuilder stringBuilder = new System.Text.StringBuilder();
			stringBuilder.Append("select ");
			if (Top > 0)
			{
				stringBuilder.Append(" top " + Top.ToString());
			}
			stringBuilder.Append(" * FROM HL_CouponItem ");
			if (strWhere.Trim() != "")
			{
				stringBuilder.Append(" where " + strWhere);
			}
			stringBuilder.Append(" order by " + filedOrder);
			return Conn.Query(stringBuilder.ToString(), new IDbDataParameter[0]);
		}
		public int GetRecordCount(string strWhere)
		{
			System.Text.StringBuilder stringBuilder = new System.Text.StringBuilder();
			stringBuilder.Append("select count(1) FROM HL_CouponItem ");
			if (strWhere.Trim() != "")
			{
				stringBuilder.Append(" where " + strWhere);
			}
			object single = DbHelperSQL.GetSingle(stringBuilder.ToString());
			int result;
			if (single == null)
			{
				result = 0;
			}
			else
			{
				result = System.Convert.ToInt32(single);
			}
			return result;
		}
		public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
		{
			System.Text.StringBuilder stringBuilder = new System.Text.StringBuilder();
			stringBuilder.Append("SELECT * FROM ( ");
			stringBuilder.Append(" SELECT ROW_NUMBER() OVER (");
			if (!string.IsNullOrEmpty(orderby.Trim()))
			{
				stringBuilder.Append("order by T." + orderby);
			}
			else
			{
				stringBuilder.Append("order by T.CouponItemID desc");
			}
			stringBuilder.Append(")AS Row, T.*  from HL_CouponItem T ");
			if (!string.IsNullOrEmpty(strWhere.Trim()))
			{
				stringBuilder.Append(" WHERE " + strWhere);
			}
			stringBuilder.Append(" ) TT");
			stringBuilder.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
			return Conn.Query(stringBuilder.ToString(), new IDbDataParameter[0]);
		}
		public DataSet GetListByItemID(string itemID)
		{
			string text = "select * from HL_CouponInfo as info,HL_CouponItem as item where info.CouponInfoID=item.CouponInfoID and item.CouponItemID in (" + itemID + ")";
			return Conn.Query(text.ToString(), new IDbDataParameter[0]);
		}
	}
}
